
#include "../include/stack.h"

#include "stdlib.h"


struct Node
{
    int capacity;
    int top;
    DataType *data;
};


Stack create_stack(int max)
{
    Stack s;

    s = (Stack)malloc(sizeof(struct Node));
    if (s)
    {
        s->data = (DataType *)malloc(sizeof(DataType) * max);
        if (s->data)
        {
            s->capacity = max;
            s->top = -1;
        }
        else
        {
            free(s);
            s = 0;
        }
    }
    
    return s;
}

void destory_stack(Stack s)
{
    if (s)
    {
        free(s->data);
        free(s);
    }
}

int stack_push(Stack s, DataType x)
{
    if (s->top + 1 >= s->capacity)  //full
    {
        return -1;
    }
    else
    {
        s->data[++s->top] = x;
    }
    
	return 0;
}

int stack_pop(Stack s, DataType *data)
{
    if (s->top < 0)     //empty
    {
        return -1;
    }
    else
    {
        *data = s->data[s->top--];
    }
    
    return 0;
}

